/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.sec;


import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Permiso;
import dp1.titandevelop.titano.service.PerfilService;
import dp1.titandevelop.titano.service.PermisoService;
import dp1.titandevelop.titano.service.ValidacionService;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;


/**
 *
 * @author chrestean
 */
public class ModulosT_SEG_APerfilNuevo extends javax.swing.JInternalFrame {

    private ArrayList<Permiso> listaPermisosPerfil;
    private ArrayList<Permiso> listaPermisosBD;
    Permiso p = new Permiso();    
    

    
   
    private AbstractTableModel modeloPermisosBD = new AbstractTableModel () { // usando una clase anonima
        // colocando los titulos de las columnas
        String [] titles = {"Descripción Permiso", "Módulo", "Opcion"}; 
        
        public String getColumnName(int col){
            return titles[col];
        }
     
        public int getRowCount() {
            return listaPermisosBD.size(); // cuantos registros (filas)
        }

        public int getColumnCount() {
            return 3; // cuantas columnas
        }

          public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            Permiso p = listaPermisosBD.get(rowIndex);
            switch(columnIndex){
                case 0: res =  p.getDescripcion(); break;
                case 1: res = p.getModulo(); break;
                case 2:  res= p.getOpcion(); break;
                case 3: res = String.valueOf(p.getIdpermiso()); break;
                       
            }
            return res;
        }
          public Permiso getValue(int row){
              return listaPermisosBD.get(row);
          }

    };
     private AbstractTableModel modeloPermisosPerfil= new AbstractTableModel () { // usando una clase anonima
        // colocando los titulos de las columnas
        String [] titles = {"Descripción Permiso", "Módulo", "Opcion"}; 
        
        public String getColumnName(int col){
            return titles[col];
        }
     
        public int getRowCount() {
            return listaPermisosPerfil.size(); // cuantos registros (filas)
        }

        public int getColumnCount() {
            return 3; // cuantas columnas
        }

          public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            Permiso p = listaPermisosPerfil.get(rowIndex);
            switch(columnIndex){
                case 0: res =  p.getDescripcion(); break;
                case 1: res = p.getModulo(); break;
                case 2:  res= p.getOpcion(); break;
                       
            }
            return res;
        }


    };
    public ModulosT_SEG_APerfilNuevo() {
        initComponents();
        Integer longitudMaximaPermitida=30;
        PermisoService pe = new PermisoService();
        ValidacionService validacionService = new ValidacionService();
       validacionService.SLetras(this.txtPerfil);
       validacionService.Longitud(this.txtPerfil, longitudMaximaPermitida); 
       this.listaPermisosBD=(ArrayList<Permiso>) pe.buscar();
        this.listaPermisosPerfil=new ArrayList<Permiso>() ;
       
        this.tablaPermisosBD.setModel(modeloPermisosBD);
        this.tablaPermisosPerfil.setModel(modeloPermisosPerfil);
        this.tablaPermisosBD.setRowSelectionAllowed(true);
        this.tablaPermisosPerfil.setRowSelectionAllowed(true);
        this.modeloPermisosBD.fireTableChanged(null);
      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblPerfil = new javax.swing.JLabel();
        txtPerfil = new javax.swing.JTextField();
        botonSalir = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JToggleButton();
        btnQuitar = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPermisosPerfil = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaPermisosBD = new javax.swing.JTable();

        setResizable(true);
        setTitle("Nuevo Perfil");
        setToolTipText("");
        setMaximumSize(new java.awt.Dimension(1130, 445));
        setMinimumSize(new java.awt.Dimension(1130, 445));
        setPreferredSize(new java.awt.Dimension(1130, 445));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        lblPerfil.setText("Descripcion");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblPerfil)
                .addGap(18, 18, 18)
                .addComponent(txtPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPerfil)
                    .addComponent(txtPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        botonSalir.setToolTipText("Salir");
        botonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSalirActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        botonGuardar.setToolTipText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Permisos"));
        jPanel2.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        btnAgregar.setText("<< Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnQuitar.setText("Quitar >>");
        btnQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Permisos Perfil"));

        tablaPermisosPerfil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Descripcion", "Modulo", "Opcion"
            }
        ));
        tablaPermisosPerfil.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaPermisosPerfilKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tablaPermisosPerfil);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Permisos Sistema"));

        tablaPermisosBD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Descripcion", "Modulo", "Opcion"
            }
        ));
        tablaPermisosBD.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaPermisosBDKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(tablaPermisosBD);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnAgregar)
                    .addComponent(btnQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(btnAgregar)
                        .addGap(62, 62, 62)
                        .addComponent(btnQuitar)
                        .addGap(0, 85, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 968, Short.MAX_VALUE)
                        .addComponent(botonGuardar)
                        .addGap(0, 0, 0)
                        .addComponent(botonSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(1, 1, 1)))
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonGuardar)
                    .addComponent(botonSalir))
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSalirActionPerformed
         ModulosT_SEG_APerfil admUser= new ModulosT_SEG_APerfil();
        this.getParent().add(admUser);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admUser.getSize();
        admUser.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height)/4 );
        
        admUser.show();
        this.dispose();
    }//GEN-LAST:event_botonSalirActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
       
       
        int[] listaseleccionada = this.tablaPermisosBD.getSelectedRows();
         for (int j = 0; j < listaseleccionada.length; j++) {           
            this.listaPermisosPerfil.add(this.listaPermisosBD.get(listaseleccionada[j]));
            this.modeloPermisosPerfil.fireTableChanged(null);         
        }
     
         for (int j = 0; j < listaseleccionada.length; j++) {          
             int pos=listaseleccionada[j]-j;
             this.listaPermisosBD.remove(pos);                   
             this.modeloPermisosBD.fireTableChanged(null);
          
        }
      

      
    }//GEN-LAST:event_btnAgregarActionPerformed
//
    private void btnQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarActionPerformed
   
     int[] listaseleccionada = this.tablaPermisosPerfil.getSelectedRows();
         for (int j = 0; j < listaseleccionada.length; j++) {           
            this.listaPermisosBD.add(this.listaPermisosPerfil.get(listaseleccionada[j]));
            this.modeloPermisosBD.fireTableChanged(null);         
        }
     
         for (int j = 0; j < listaseleccionada.length; j++) {          
             int pos=listaseleccionada[j]-j;
             this.listaPermisosPerfil.remove(pos);                   
             this.modeloPermisosPerfil.fireTableChanged(null);
          
        }
      
    }//GEN-LAST:event_btnQuitarActionPerformed

    private void tablaPermisosBDKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaPermisosBDKeyPressed
             if (evt.getKeyCode()==KeyEvent.VK_LEFT){this.btnAgregar.doClick();
}
    }//GEN-LAST:event_tablaPermisosBDKeyPressed

    private void tablaPermisosPerfilKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaPermisosPerfilKeyPressed
        if (evt.getKeyCode()==KeyEvent.VK_RIGHT){this.btnQuitar.doClick();
}
    }//GEN-LAST:event_tablaPermisosPerfilKeyPressed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
       PerfilService ps = new PerfilService();
       Mensajes mensajes = new Mensajes();
       ValidacionService validacionservice = new ValidacionService();
       if (validacionservice.esVacio(this.txtPerfil)){
              JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getCompletarCampos(),"Aviso",JOptionPane.WARNING_MESSAGE);
       }
       else {
           try {
           ps.crearPerfil(this.txtPerfil.getText(), listaPermisosPerfil);
           JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getRegistrado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);
           
           ModulosT_SEG_APerfil admUser= new ModulosT_SEG_APerfil();
          this.getParent().add(admUser);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admUser.getSize();
        admUser.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height)/4 );
          //JOptionPane.showMessageDialog(this, mensajes.getRegistrado(),"Creacion de Perfil",
            //        JOptionPane.INFORMATION_MESSAGE);
        admUser.show();
        this.dispose();
           
           
       }
       catch (Exception e) {
            JOptionPane.showMessageDialog(this, mensajes.getError(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
       }
       }
       
       
    }//GEN-LAST:event_botonGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonSalir;
    private javax.swing.JToggleButton btnAgregar;
    private javax.swing.JToggleButton btnQuitar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblPerfil;
    private javax.swing.JTable tablaPermisosBD;
    private javax.swing.JTable tablaPermisosPerfil;
    private javax.swing.JTextField txtPerfil;
    // End of variables declaration//GEN-END:variables

    
}
